home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 1338 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  101 lines

  1. Path: su3.in.net!news
  2. From: mave@in.net (John J. Maver, Jr.)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: My Commafy function sucks!! Help me please?
  5. Date: 18 Jan 1996 04:50:05 GMT
  6. Organization: INTERNET Indiana
  7. Message-ID: <1934.6590T1402T2621@in.net>
  8. Reply-To: mave@in.net
  9. NNTP-Posting-Host: pm3-25.in.net
  10. X-Newsreader: THOR 2.22 (Amiga;TCP/IP)
  11.  
  12.  
  13.     I am really rusty on C, but I am trying to relearn it.  I am writing a
  14. function that takes a float or double string, and puts commas into it in the
  15. right places and then outputs as string again.
  16.     I am pretty close, I think.  But I am mixing up my languages in my mind.
  17. And I have forgotten a great deal of C.  Here is what I have so far:
  18.  
  19. /**************************************************************************/
  20. /***********************Commafy.c******************************************/
  21. /**************************************************************************/
  22.  
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29. void commafy(char *numstring)
  30. {
  31.     double floatnum,decpart;
  32.     int length,i,k,intpart;
  33.  
  34.     char intstring[100];
  35.     char decstring[100];
  36.  
  37.     intstring[0]=0;
  38.     decstring[0]=0;
  39.  
  40.     /*divide number into integer and decimal parts*/
  41.     floatnum=atof(numstring);
  42.     intpart=(int)floatnum;
  43.     decpart=floatnum-intpart;
  44.  
  45.     /*put commas into integer portion*/
  46.  
  47.     sprintf(intstring,"%s",intpart);
  48.  
  49.     length=strlen(intstring);
  50.     i=length;
  51.  
  52.     while ((i - 3) > 0)
  53.     {
  54.         i=i-3;
  55.         for (k=length;k > i;k--)
  56.                 intstring[k+1]=intstring[k];
  57.         intstring[i]=',';
  58.     }
  59.     length=strlen(intstring);
  60.     intstring[length+1]='.';
  61.     intstring[length+2]='\0';
  62.  
  63.     /* put integer part back together with decimal portion*/
  64.  
  65.     sprintf("decstring","%s",decpart);
  66.  
  67.     strcat(intstring,decstring);
  68.  
  69.     strcpy(numstring,intstring);
  70. }
  71.  
  72.  
  73.  
  74. void main()
  75. {
  76.     char numstr[200];
  77.  
  78.  
  79.     printf("enter->");
  80.     scanf("%s",numstr);
  81.     commafy(numstr);
  82.     printf("The commafied number is%s",numstr);
  83. }
  84.  
  85.  
  86.  
  87. /****************************************************************************/
  88.  
  89.     Any suggestions?
  90.  
  91.  
  92.  
  93.  
  94. --
  95. <tsb> John J. Maver, Jr.
  96. <sb> mave@in.net
  97. <sb> A4000/040
  98. It is impossible to enjoy idling thoroughly unless one has plenty of work to
  99. do.
  100.  
  101.